-
Notifications
You must be signed in to change notification settings - Fork 1k
Correct handling of __name__ in firestore indexes #8862
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Refactored __name__ field filtering from listIndexes into static method FirestoreApi.processIndexes() to ensure tests verify production code rather than duplicating the implementation.
Simplified the filter expression in processIndexes() to a single boolean expression as suggested in PR feedback for better readability and conciseness.
… and it is the last field
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Summary of Changes
Hello @joehan, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!
This pull request addresses and fixes an issue in Firestore index filtering where __name__
fields were incorrectly handled, leading to problems like duplicate index entries and deployment conflicts. The changes ensure that index listings accurately reflect the documented behavior, specifically by preserving __name__
fields with explicit DESCENDING
order while filtering out implicit ASCENDING
ones.
Highlights
- Firestore Index Filtering Logic: Introduced a new static method
FirestoreApi.processIndexes
insrc/firestore/api.ts
to correctly filter__name__
fields in Firestore index listings. This method removes implicit__name__
fields (those withASCENDING
order matching the preceding field's order) while preserving explicit__name__
fields (e.g., those withDESCENDING
order). - Index Listing Integration: The
listIndexes
method insrc/firestore/api.ts
now utilizes theFirestoreApi.processIndexes
utility to ensure all returned index specifications adhere to the corrected filtering rules, aligning with documented behavior. - Comprehensive Testing: Added a new test suite
IndexListingWithNameFields
insrc/firestore/indexes.spec.ts
with multiple test cases to validate theprocessIndexes
logic, covering various scenarios including different__name__
field orders and ensuring proper distinction between indexes.
Using Gemini Code Assist
The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.
Invoking Gemini
You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command>
or @gemini-code-assist <command>
. Below is a summary of the supported commands.
Feature | Command | Description |
---|---|---|
Code Review | /gemini review |
Performs a code review for the current pull request in its current state. |
Pull Request Summary | /gemini summary |
Provides a summary of the current pull request in its current state. |
Comment | @gemini-code-assist | Responds in comments when explicitly tagged, both in issue comments and review comments. |
Help | /gemini help |
Displays a list of available commands. |
Customization
To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/
folder in the base of the repository. Detailed instructions can be found here.
Limitations & Feedback
Gemini Code Assist is currently in preview and may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments to provide feedback.
You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.
Footnotes
-
Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request corrects the handling of implicit __name__
fields in Firestore indexes by filtering them out. The implementation has a critical bug that can cause a crash when processing an index with an empty fields
array, and it also incorrectly handles indexes that only contain a __name__
field. Additionally, the new tests are missing coverage for this single-field __name__
index scenario.
it("should keep __name__ fields with DESCENDING order, when the default is ASCENDING", () => { | ||
const mockIndexes: API.Index[] = [ | ||
{ | ||
name: "/projects/myproject/databases/(default)/collectionGroups/collection/indexes/abc123", | ||
queryScope: API.QueryScope.COLLECTION, | ||
fields: [ | ||
{ fieldPath: "foo", order: API.Order.ASCENDING }, | ||
{ fieldPath: "__name__", order: API.Order.DESCENDING }, | ||
], | ||
state: API.State.READY, | ||
}, | ||
]; | ||
|
||
const result = FirestoreApi.processIndexes(mockIndexes); | ||
|
||
expect(result[0].fields).to.have.length(2); | ||
expect(result[0].fields[0].fieldPath).to.equal("foo"); | ||
expect(result[0].fields[1].fieldPath).to.equal("__name__"); | ||
expect(result[0].fields[1].order).to.equal(API.Order.DESCENDING); | ||
}); | ||
|
||
it("should keep __name__ fields with ASCENDING order, when the default is DESCENDING", () => { | ||
const mockIndexes: API.Index[] = [ | ||
{ | ||
name: "/projects/myproject/databases/(default)/collectionGroups/collection/indexes/abc123", | ||
queryScope: API.QueryScope.COLLECTION, | ||
fields: [ | ||
{ fieldPath: "foo", order: API.Order.DESCENDING }, | ||
{ fieldPath: "__name__", order: API.Order.ASCENDING }, | ||
], | ||
state: API.State.READY, | ||
}, | ||
]; | ||
|
||
const result = FirestoreApi.processIndexes(mockIndexes); | ||
|
||
expect(result[0].fields).to.have.length(2); | ||
expect(result[0].fields[0].fieldPath).to.equal("foo"); | ||
expect(result[0].fields[1].fieldPath).to.equal("__name__"); | ||
expect(result[0].fields[1].order).to.equal(API.Order.ASCENDING); | ||
}); | ||
|
||
it("should distinguish between indexes that differ only by __name__ order", () => { | ||
const mockIndexes: API.Index[] = [ | ||
{ | ||
name: "/projects/myproject/databases/(default)/collectionGroups/collection/indexes/abc123", | ||
queryScope: API.QueryScope.COLLECTION, | ||
fields: [ | ||
{ fieldPath: "foo", order: API.Order.ASCENDING }, | ||
{ fieldPath: "__name__", order: API.Order.ASCENDING }, | ||
], | ||
state: API.State.READY, | ||
}, | ||
{ | ||
name: "/projects/myproject/databases/(default)/collectionGroups/collection/indexes/def456", | ||
queryScope: API.QueryScope.COLLECTION, | ||
fields: [ | ||
{ fieldPath: "foo", order: API.Order.ASCENDING }, | ||
{ fieldPath: "__name__", order: API.Order.DESCENDING }, | ||
], | ||
state: API.State.READY, | ||
}, | ||
]; | ||
|
||
const result = FirestoreApi.processIndexes(mockIndexes); | ||
|
||
// First index should have __name__ field filtered out | ||
expect(result[0].fields).to.have.length(1); | ||
expect(result[0].fields[0].fieldPath).to.equal("foo"); | ||
|
||
// Second index should keep __name__ field with DESCENDING order | ||
expect(result[1].fields).to.have.length(2); | ||
expect(result[1].fields[0].fieldPath).to.equal("foo"); | ||
expect(result[1].fields[1].fieldPath).to.equal("__name__"); | ||
expect(result[1].fields[1].order).to.equal(API.Order.DESCENDING); | ||
|
||
// The two processed indexes should be different (fixing the duplicate issue) | ||
expect(JSON.stringify(result[0].fields)).to.not.equal(JSON.stringify(result[1].fields)); | ||
}); | ||
|
||
it("should handle indexes with no __name__ fields", () => { | ||
const mockIndexes: API.Index[] = [ | ||
{ | ||
name: "/projects/myproject/databases/(default)/collectionGroups/collection/indexes/abc123", | ||
queryScope: API.QueryScope.COLLECTION, | ||
fields: [ | ||
{ fieldPath: "foo", order: API.Order.ASCENDING }, | ||
{ fieldPath: "bar", arrayConfig: API.ArrayConfig.CONTAINS }, | ||
], | ||
state: API.State.READY, | ||
}, | ||
]; | ||
|
||
const result = FirestoreApi.processIndexes(mockIndexes); | ||
|
||
expect(result[0].fields).to.have.length(2); | ||
expect(result[0].fields[0].fieldPath).to.equal("foo"); | ||
expect(result[0].fields[1].fieldPath).to.equal("bar"); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new tests cover composite indexes well, but they miss an important edge case: an index that only contains the __name__
field. Add test cases to verify the behavior for single-field __name__
indexes.
it("should filter out a single __name__ ASCENDING field", () => {
const mockIndexes: API.Index[] = [
{
name: "/projects/myproject/databases/(default)/collectionGroups/collection/indexes/abc123",
queryScope: API.QueryScope.COLLECTION,
fields: [
{ fieldPath: "__name__", order: API.Order.ASCENDING },
],
state: API.State.READY,
},
];
const result = FirestoreApi.processIndexes(mockIndexes);
expect(result[0].fields).to.have.length(0);
});
it("should keep a single __name__ DESCENDING field", () => {
const mockIndexes: API.Index[] = [
{
name: "/projects/myproject/databases/(default)/collectionGroups/collection/indexes/abc123",
queryScope: API.QueryScope.COLLECTION,
fields: [
{ fieldPath: "__name__", order: API.Order.DESCENDING },
],
state: API.State.READY,
},
];
const result = FirestoreApi.processIndexes(mockIndexes);
expect(result[0].fields).to.have.length(1);
expect(result[0].fields[0].fieldPath).to.equal("__name__");
expect(result[0].fields[0].order).to.equal(API.Order.DESCENDING);
});
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM! Tested multiple deployments for each scenario
- Deploy does not ask deletions when prod and local indexes match (OK)
- Deploy correctly creates new indexes if they don't exist in prod (OK)
- Deploying with
__name__
explecitly set (OK)
Edit: Tested deploying to a new project(OK)
Description
Fix index filtering to match the documented behavior. Builds off of #8860 - thank you for the help!
Fixes Fixes #7629, #8859
Scenarios Tested
Tested using the scenario @aalej set up in #8860 (comment) - it now works on repeated deployments!